package com.glsc.ngateway.platform.service.glres;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscResourceOperateLog;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.glres.GlscResourceOperateLogRepository;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

@Service
public class GlscResourceOperateLogService {

    @Resource
    private GlscResourceOperateLogRepository operateLogRepository;

    @Resource
    private UserService userService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<GlscResourceOperateLog> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscResourceOperateLog> page = operateLogRepository.findAll(SpecificationUtil.buildSpecification(param, GlscResourceOperateLog.class), pageable);
        return page;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public GlscResourceOperateLog findById(Integer id) {
        Optional<GlscResourceOperateLog> optional = operateLogRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public GlscResourceOperateLog save(GlscResourceOperateLog obj) {
        return operateLogRepository.save(obj);
    }

}
